package com.lt.view;

import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;

import com.lt.base.BaseApplication;
import com.xy.lib.R;

/**
 * Created by tianlan on 2018/1/23.
 */

public class AnimationHelper {

    private static AnimationHelper instance;

    public static AnimationHelper getInstance() {
        if (instance == null) {
            synchronized (AnimationHelper.class) {
                instance = new AnimationHelper();
            }
        }
        return instance;
    }

    private AnimationHelper() {

    }

    /**
     * 闪烁（透明度亮 -> 暗， 暗->亮，如此循环）
     *
     * @param view
     * @param fromAlpha
     * @param toAlpha
     * @param duration
     */
    public void twinkle(View view, float fromAlpha, float toAlpha, long duration) {
        //        闪烁
        AlphaAnimation alphaAnimation = new AlphaAnimation(fromAlpha, toAlpha);
        alphaAnimation.setDuration(duration);
        alphaAnimation.setRepeatCount(Animation.INFINITE);
        alphaAnimation.setRepeatMode(Animation.REVERSE);
//        view.setAnimation(alphaAnimation);
//        alphaAnimation.start();
        view.startAnimation(alphaAnimation);
    }


    /**
     * 图片旋转 fromDegrees:图片旋转的开始位置 toDegress:图片旋转的结束位置
     */
    public void rotate(View view, int time, float fromDegrees, float toDegress) {
        final float centerX = view.getWidth() / 2.0f;
        final float centerY = view.getHeight() / 2.0f;
        RotateAnimation rotateAnimation = new RotateAnimation(fromDegrees, toDegress, centerX,
                centerY);
        // centerX表示动画的旋转中心x轴，centerY表示动画旋转中心y轴。
        rotateAnimation.setDuration(time);
        // 动画持续时间
        rotateAnimation.setFillAfter(true);
        // ture表示动画结束后停留在动画的最后位置，false表示动画结束后回到初始位置，默认为false。
        view.startAnimation(rotateAnimation);
    }

    /**
     * @return 菊花旋转动画
     */
    public Animation getProgressAnimation() {
        return AnimationUtils.loadAnimation(BaseApplication.getContext(),
                R.anim.load_animation);
    }

    public void shake(final View view) {
        Animation anim = AnimationUtils.loadAnimation(BaseApplication.getContext(), R.anim.animation_shake);
        view.startAnimation(anim);
    }

    public ObjectAnimator tada(View view) {
        return tada(view, 1f);
    }

    public ObjectAnimator tada(View view, float shakeFactor) {

        PropertyValuesHolder pvhScaleX = PropertyValuesHolder.ofKeyframe(View.SCALE_X,
                Keyframe.ofFloat(0f, 1f),
                Keyframe.ofFloat(.1f, .9f),
                Keyframe.ofFloat(.2f, .9f),
                Keyframe.ofFloat(.3f, 1.1f),
                Keyframe.ofFloat(.4f, 1.1f),
                Keyframe.ofFloat(.5f, 1.1f),
                Keyframe.ofFloat(.6f, 1.1f),
                Keyframe.ofFloat(.7f, 1.1f),
                Keyframe.ofFloat(.8f, 1.1f),
                Keyframe.ofFloat(.9f, 1.1f),
                Keyframe.ofFloat(1f, 1f)
        );

        PropertyValuesHolder pvhScaleY = PropertyValuesHolder.ofKeyframe(View.SCALE_Y,
                Keyframe.ofFloat(0f, 1f),
                Keyframe.ofFloat(.1f, .9f),
                Keyframe.ofFloat(.2f, .9f),
                Keyframe.ofFloat(.3f, 1.1f),
                Keyframe.ofFloat(.4f, 1.1f),
                Keyframe.ofFloat(.5f, 1.1f),
                Keyframe.ofFloat(.6f, 1.1f),
                Keyframe.ofFloat(.7f, 1.1f),
                Keyframe.ofFloat(.8f, 1.1f),
                Keyframe.ofFloat(.9f, 1.1f),
                Keyframe.ofFloat(1f, 1f)
        );

        PropertyValuesHolder pvhRotate = PropertyValuesHolder.ofKeyframe(View.ROTATION,
                Keyframe.ofFloat(0f, 0f),
                Keyframe.ofFloat(.1f, -3f * shakeFactor),
                Keyframe.ofFloat(.2f, -3f * shakeFactor),
                Keyframe.ofFloat(.3f, 3f * shakeFactor),
                Keyframe.ofFloat(.4f, -3f * shakeFactor),
                Keyframe.ofFloat(.5f, 3f * shakeFactor),
                Keyframe.ofFloat(.6f, -3f * shakeFactor),
                Keyframe.ofFloat(.7f, 3f * shakeFactor),
                Keyframe.ofFloat(.8f, -3f * shakeFactor),
                Keyframe.ofFloat(.9f, 3f * shakeFactor),
                Keyframe.ofFloat(1f, 0)
        );
        return ObjectAnimator.ofPropertyValuesHolder(view, pvhScaleX, pvhScaleY, pvhRotate).
                setDuration(1000);
    }
}
